﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace DDS.IoT.Modbus
{
    class Program
    {
        static void Main(string[] args)
        {
            RtuTest();
            //TcpTest();
            Console.ReadLine();
        }

        private static void TcpTest()
        {
            ModbusTcpCom driver = new ModbusTcpCom();
            Console.Write("请输入端口号：");
            var port = Convert.ToInt32(Console.ReadLine());//502
            driver.Init(port, extendData:"127.0.0.1");
            driver.Open();
            var data = driver.Command("ReadCoils", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, numberOfPoints = 10 }));
            Console.WriteLine("01读取的数据：" + data);
            data = driver.Command("ReadInputs", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, numberOfPoints = 10 }));
            Console.WriteLine("02读取的数据：" + data);
            data = driver.Command("ReadHoldingRegisters", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, numberOfPoints = 10 }));
            Console.WriteLine("03读取的数据：" + data);
            data = driver.Command("ReadInputRegisters", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, numberOfPoints = 10 }));
            Console.WriteLine("04读取的数据：" + data);

            driver.Command("WriteSingleCoil", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, value = 1 }));
            driver.Command("WriteSingleRegister", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, value = 123 }));
            driver.Command("WriteMultipleCoils", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, value = "1 0 1" }));
            driver.Command("WriteMultipleRegisters", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, value = "10 20 30" }));
        }

        private static void RtuTest()
        {
            ModbusRtuCom driver = new ModbusRtuCom();
            Console.Write("请输入串口号：");
            var port =Convert.ToInt32(Console.ReadLine());// 2
            driver.Init(port);
            driver.Open();

            var data = driver.Command("ReadCoils", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, numberOfPoints = 10 }));
            Console.WriteLine("01读取的数据：" + data);
            data = driver.Command("ReadInputs", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, numberOfPoints = 10 }));
            Console.WriteLine("02读取的数据：" + data);
            data = driver.Command("ReadHoldingRegisters", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, numberOfPoints = 10 }));
            Console.WriteLine("03读取的数据：" + data);
            data = driver.Command("ReadInputRegisters", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, numberOfPoints = 10 }));
            Console.WriteLine("04读取的数据：" + data);

            driver.Command("WriteSingleCoil", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, value = 1 }));
            driver.Command("WriteSingleRegister", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, value = 123 }));
            driver.Command("WriteMultipleCoils", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, value = "1 0 1" }));
            driver.Command("WriteMultipleRegisters", Newtonsoft.Json.JsonConvert.SerializeObject(new { slaveAddress = "1", startAddress = 0, value = "10 20 30" }));
        }
    }
}
